#!/bin/sh

. /etc/rc.status

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/mono/mono-service2
NAME=sdcloud-modelrun-agent

MONOSERVER=`which mono-service`
MONOSERVER_PID=$(cat /tmp/$NAME.lock)

SERVICE_DEPLOY_DIR=__(SdCloud.ModelRunAgent.DeployDir)__

case "$1" in
 start)
  echo "Starting $NAME"
  if [ -z "${MONOSERVER_PID}" ]; then
   ${MONOSERVER} -l:/tmp/$NAME.lock -d:$SERVICE_DEPLOY_DIR $SERVICE_DEPLOY_DIR/SDCloudSolution.SDCloud.ModelRunAgent.exe
   rc_exit
  else
   echo "$NAME is already running!"
   rc_failed
  fi
 ;;
 stop)
  echo "Stopping $NAME"
  if [ -n "${MONOSERVER_PID}" ]; then
   kill ${MONOSERVER_PID}
   rm /tmp/$NAME.lock
   rc_exit
  else
   echo "$NAME is not running"
   rc_failed
  fi
 ;;
 restart)
  $0 stop
  sleep 1
  $0 start
  rc_exit
 ;;
 *)
  echo "usage: $0 {start|stop|restart}"
esac

exit 0